package com.example.service.impl;

import com.example.service.HttpService;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
public class HttpServiceImpl implements HttpService {
    @Override
    public String sendPost(String url, Map<String, String> header, Map<String, Object> body) {
        //使用Restemplate来发送HTTP请求
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("accept", "*/*");
//        headers.setA;
        HttpEntity httpEntity = new HttpEntity(body,headers);
        try {
            //使用 exchange 发送请求，以String的类型接收返回的数据
            ResponseEntity<String> strbody = restTemplate.exchange(url, HttpMethod.POST,httpEntity,String.class);
            //解析返回的数据
//            String reBody = strbody.getBody();
//            JSONObject jsonObject = JSONObject.parseObject(reBody);
            return strbody.getBody();
        }catch (Exception e){
            System.out.println(e);
        }
        return "发送失败";
    }
}
